class User < ActiveRecord::Base
  include BCrypt

  attr_accessible :email, :password

  validates :email, :uniqueness => true,
                    :format => /\A[^@]+@[^@]+\z/

  def password
    nil
  end

  def password=(plain_text)
    self.encrypted_password = Password.create(PASSWORD_SECRET_KEY + plain_text)
  end

  def password_valid?(plain_text)
    return false unless encrypted_password
    Password.new(encrypted_password) == PASSWORD_SECRET_KEY + plain_text
  end

  def authenticatable_salt
    encrypted_password && encrypted_password[-31..-1]
  end
end